/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools command
 */

#ifndef HMFS_COMMAND_H
#define HMFS_COMMAND_H

#include <functional>
#include <limits>
#include <map>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <vector>

#include "constant.h"
#include "hmfs_utils.h"
namespace OHOS {
namespace Hmfs {
struct CmdConfig {
    CmdFunc func;
    bool heapBasedAllocation = false;
    bool forceOverwrite = false;
    bool zonedMode = false;
    std::vector<std::string> deviceList;
    int32_t debugLevel = 0;
    uint64_t wantedSectorSize = std::numeric_limits<uint64_t>::max();
    uint64_t wantedSectorCount = std::numeric_limits<uint64_t>::max();
    uint32_t features = 0;
    uint32_t segsPerSection = 1;
    uint32_t sectionsPerZone = 1;
    double overProvision = 0.0;
    std::vector<std::string> coldExtList;
    std::vector<std::string> hotExtList;
    std::string volume;
    int32_t force = 0;
    int32_t safeResize = 0;
    int32_t largeNatBitmap = 0;
    uint16_t sEncoding = 0;
    uint16_t sEncodingFlags = 0;
    double newOverprovision;
    uint32_t rootUid = getuid();
    uint32_t rootGid = getgid();
    uint64_t targetSectors = 0;
    uint32_t quotaBits;
    bool trim = true;
    uint32_t defaultOptionSet = 0;
    bool fakeSeed = false;
    uint32_t timeStamp = std::numeric_limits<uint32_t>::max();
    std::string uuid;
};

class CmdParser {
public:
    ~CmdParser() = default;
    ArgParseResult ParseOption(int32_t argc, char *argv[]);
    virtual void ShowCmdUsage() = 0;
    using HandleArgValue = std::function<ArgParseResult(const std::string &)>;
    void RegCmdOption(char option, bool hasPara, HandleArgValue handle);
    ArgParseResult ProcessOption(char option, const std::string &argValue);
    ArgParseResult ProcessCasefolding(const std::string &argValue);
    ArgParseResult ProcessDebugLevel(const std::string &argValue);
    ArgParseResult ProcessFeatures(const std::string &argValue);
    ArgParseResult ProcessOverProvision(const std::string &argValue);
    ArgParseResult ProcessLargeNatBitmap(const std::string &argValue);

    CmdConfig &GetCmdConfig()
    {
        return cfgPara_;
    }
    static CmdParser &GetSingleton()
    {
        return *instance_;
    }
    static int32_t GetDebugLevel()
    {
        if (instance_ == nullptr) {
            return -1;
        }

        return instance_->cfgPara_.debugLevel;
    }

protected:
    CmdConfig &cfgPara_;
    explicit CmdParser(CmdConfig &cmdPara) : cfgPara_(cmdPara)
    {
        instance_ = this;
    }

private:
    std::map<char, HandleArgValue> handles_;
    std::string optionString_;
    static CmdParser *instance_;
};

} // namespace Hmfs
} // namespace OHOS
#endif
